package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.vo.RefundVo;
import com.gdut.huayan.service.RefundService;
import com.gdut.huayan.validation.NoLog;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.groups.Default;

@RestController
@RequestMapping("/refund")
public class RefundController {

    @Autowired
    private RefundService refundService;

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/submit")
    public Result addRefund(@RequestBody @Validated({GroupAdd.class,Default.class}) RefundVo refundVo){
        refundService.submitRefund(refundVo);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/getAll")
    public Result getAll(@RequestBody PageQuery pageQuery, Integer status){
        return Result.success(refundService.getAll(pageQuery,status));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/detail")
    public Result getRefundDetail(Long refundId,Long orderId){
        return Result.success(refundService.getRefundDetail(refundId,orderId));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/check")
    public Result updateRefund(@RequestBody @Validated({GroupUpdate.class,Default.class}) RefundVo refundVo){
        refundService.checkRefund(refundVo);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/get")
    public Result get(Integer status){
        return Result.success(refundService.getByUser(status));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @DeleteMapping("/cancel")
    public Result cancelRefund(Long refundId){
        return Result.success(refundService.removeById(refundId));
    }

    @NoLog
    @PreAuthorize("permitAll()")
    @PostMapping("/callback/df698gtiu31s8fhp")
    public void callBackRefund(HttpServletRequest httpServletRequest){
        refundService.callBack(httpServletRequest);
    }
}
